package com.hxy.order.service.impl;

import com.hxy.basic.service.impl.BaseServiceImpl;
import com.hxy.order.domain.Product;
import com.hxy.order.domain.ProductDetail;
import com.hxy.order.mapper.ProductDetailMapper;
import com.hxy.order.mapper.ProductMapper;
import com.hxy.order.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService{
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    /**
     * 上架
     */
    @Override
    public void onSale(List<Long> ids) {
        Map<String,Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("onsaletime",new Date());
        //1.修改state状态 = 1
        //2.设置上架时间onsaletime = new Date();
        productMapper.onSale(map);

    }

    /**
     * 下架
     */
    @Override
    public void offSale(List<Long> ids) {
        //1.修改state状态 = 0
        //2.设置下架时间offsaletime= new Date();
        Map<String,Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("offsaletime",new Date());
        productMapper.offSale(map);
    }
    
    /**
     * 修改
     */
    @Override
    @Transactional
    public void update(Product product) {
        //修改宠物的信息
        super.update(product);
        //修改宠物详情
        ProductDetail detail = product.getProductDetail();
        if(detail!= null){
            detail.setProduct_id(product.getId());
            productDetailMapper.updateByProductId(detail);
        }
    }
    /**
     * 保存
     */
    @Override
    public void save(Product product) {
        //添加宠物信息
        super.save(product);
        //添加明细
        ProductDetail detail = product.getProductDetail();
        if(detail != null){
            detail.setProduct_id(product.getId());
            productDetailMapper.save(detail);
        }

    }
}
